import 'package:elevator/get/intl_keys.dart';
import 'package:elevator/get/message.dart';
import 'package:elevator/model/build_info.dart';
import 'package:elevator/model/lift_info.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';

import 'third_logic.dart';

class ThirdPage extends StatefulWidget {

  BuildInfo buildInfo;
  LiftInfo liftInfo;

  ThirdPage(this.buildInfo, this.liftInfo, {super.key});

  @override
  State<StatefulWidget> createState() {
    return _ThirdPageState(buildInfo,liftInfo);
  }
}

class _ThirdPageState extends State<StatefulWidget>{
  late Size size;
  double itemWidth = 60;
  late ThirdLogic logic;

  _ThirdPageState(BuildInfo buildInfo, LiftInfo liftInfo){
    Get.delete<ThirdLogic>();
    logic = Get.put(ThirdLogic(buildInfo,liftInfo));
  }

  @override
  Widget build(BuildContext context) {
    size = MediaQuery
        .of(context)
        .size;
    itemWidth = (size.width - 12 * 2) / 4;
    return Padding(padding: const EdgeInsets.all(10),
      child: SingleChildScrollView(
        scrollDirection: Axis.horizontal,
        child: Container(
          color: Colors.white,
          child: Obx(()=> DataTable(
              horizontalMargin:0,
              columnSpacing: 0,
              border: TableBorder.all(width: 1, color: Colors.grey),
              columns: [
                DataColumn(
                    label: Container(width:itemWidth*3,alignment: Alignment.center,child: Text(IntlKeys.emergencyInformation.tr),)
                ),
                DataColumn(
                    label: Container(width:itemWidth,alignment: Alignment.center,child: Text(IntlKeys.signStatus.tr),)
                ),

                DataColumn(
                    label: Container(width:itemWidth*3,alignment: Alignment.center,child: Text(IntlKeys.emergencyInformation.tr),)
                ),
                DataColumn(
                    label: Container(width:itemWidth,alignment: Alignment.center,child: Text(IntlKeys.signStatus.tr),)
                ),
              ], rows: buildRows(logic.buildInfo,logic.liftInfo.value,itemWidth)),)
        ),
      ),);
  }

  buildRows(BuildInfo buildInfo, LiftInfo liftInfo,double itemWidth) {
    bool isCz = Messages.isCzLocal;
    List<DataRow> rows = [];
    for(int i=0;i<16;i++){
      String? svpColum1,svpColum2;
      if(buildInfo.warnInfoMap.containsKey(i+1)){
        svpColum1 = isCz? buildInfo.warnInfoMap[i+1]?.lNameCn:buildInfo.warnInfoMap[i+1]?.lNameEn;
      }
      if(buildInfo.warnInfoMap.containsKey(i+1+16)){
        svpColum2 = isCz? buildInfo.warnInfoMap[i+1+16]?.lNameCn:buildInfo.warnInfoMap[i+1+16]?.lNameEn;
      }

      bool on1 = false,on2 = false;
      if(null!=liftInfo.svpStr && liftInfo.svpStr!.isNotEmpty){
        if(i<liftInfo.svpStr!.length){
          String char = liftInfo.svpStr!.substring(i,i+1);
          if(char.compareTo("0")==0){
            on1 = true;
          }else{
            on1 = false;
          }
        }

        if(i<liftInfo.svpStr!.length){
          String char = liftInfo.svpStr!.substring(i+16,i+17);
          if(char.compareTo("0")==0){
            on2 = true;
          }else{
            on2 = false;
          }
        }
      }

      DataRow dataRow = DataRow(cells: [
        DataCell(Container(
            alignment: Alignment.center,
            child:Row(children: [
              Flexible(
                flex: 1,
                child: Container(alignment: Alignment.center,child:Text("LED${i+1}")),),
              const VerticalDivider(width: 1,color: Colors.grey,),
              Flexible(
                  flex: 2,
                  child: Container(alignment: Alignment.center,child:Text(svpColum1 ?? ""),)),
            ],))
        ),

        DataCell(Container(alignment: Alignment.center,color: on1?Colors.yellow:Colors.transparent,child:Text(on1? "ON":""))),

        DataCell(Container(
            alignment: Alignment.center,
            child:Row(children: [
              Flexible(
                flex: 1,
                child: Container(alignment: Alignment.center,child:Text("LED${i+17}")),),
              const VerticalDivider(width: 1,color: Colors.grey),
              Flexible(
                  flex: 2,
                  child: Container(alignment: Alignment.center,child:Text(svpColum2 ?? ""),)),
            ],))
        ),

        DataCell(Container(alignment: Alignment.center,color: on2?Colors.yellow:Colors.transparent,child:Text(on2? "ON":""))),
      ],);
      rows.add(dataRow);
    }
    return rows;

  }

  @override
  void dispose() {
    logic.unregister();
    super.dispose();
  }

  @override
  void initState() {
    logic.register();
    super.initState();
  }

}
